package hk.m4s.lr.repair.test.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.PushModel;
import hk.m4s.lr.repair.test.model.PushModelResult;
import hk.m4s.lr.repair.test.service.notice.NoticeService;
import hk.m4s.lr.repair.test.ui.user.AppSetActivity;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/3/16.
 */

public class PushListAdapter extends BaseAdapter{
    List<PushModel> list=new ArrayList<PushModel>();
    Context context;
    LayoutInflater inflater;

    public PushListAdapter(Context context, List<PushModel> data){
        this.context=context;
        this.list=data;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            holder=new ViewHolder();
            convertView=inflater.inflate(R.layout.list_item_push,parent,false);
            holder.push_name=(TextView)convertView.findViewById(R.id.push_name);
            holder.push_on=(Button) convertView.findViewById(R.id.push_on);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        holder.push_on.setTag(position);
          PushModel deviceModel = list.get(position);
            try {
                holder.push_name.setText(deviceModel.getName());
                if(deviceModel.getState()==1){
                    holder.push_on.setBackgroundResource(R.mipmap.set_ico_yes);
                }else{
                    holder.push_on.setBackgroundResource(R.mipmap.set_ico_no);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        holder.push_on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=(Integer) v.getTag();
                PushModel deviceModel = list.get(num);
                if(deviceModel.getState()==1) {
                    deviceModel.setState(-1);
                }else{
                    deviceModel.setState(1);
                }

                list.set(num,deviceModel);
                userPushSetUpdate(list.get(num).getId(),list.get(num).getState());
                notifyDataSetChanged();

            }
        });
        return convertView;
    }

    public static class ViewHolder{
        TextView push_name;
        Button push_on;
    }
    public void userPushSetUpdate(int id,int state) {
        try {
            Map<String, Object> parmas = new HashMap<>();
            parmas.put("device_type", Constant.device_type);
            parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
            JSONObject jsondata = new JSONObject();
            jsondata.put("id", id);
            jsondata.put("state", state);
            parmas.put("jsonText", jsondata.toString());
            NoticeService.userPushSetUpdate(context, parmas, new ResponseCallback<Map>() {
                @Override
                public void onSuccess(Map deviceModelResult) {
                }

                @Override
                public void onError(Response response, int errorCode, String errorMsg, Exception e) {

                }
            });
        }catch (JSONException e){
            e.printStackTrace();
        }
    }
}
